﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using EvaluationSystem.DAO;
using System.Data;

namespace EvaluationSystem
{
    public partial class EvaluationPage : System.Web.UI.Page
    {
        const string EmptyTabId = "EmptyTabId";
        EvaluationManager evaluationManager = new EvaluationManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Utility.IsLogin(this.Page))
            {
#if !DEBUG
                Response.Redirect("Default.aspx");
#endif
            }
            if (!X.IsAjaxRequest)
            {
                if (IsNoRole())
                {
                    CreateEmptyTab();
                }
                else 
                {
                    CreateTabs();
                }
            }
        }
        protected bool IsNoRole()
        {
            Roles role = Utility.Role(this.Page);
            return role == Roles.None;
        }

        private Ext.Net.Panel CreateTab(string id,string title)
        {
            Ext.Net.Panel panel = new Ext.Net.Panel();
            panel.ID = id;
            panel.Title = title;
            panel.Padding = 6;
            panel.AutoScroll = true;
            panel.Closable = false;
            return panel;
        }

        protected void CreateEmptyTab()
        {
            Ext.Net.Panel panel = CreateTab(EmptyTabId,Constants.Noticfy);
            Ext.Net.Label msg = new Ext.Net.Label
            {
                Html = "当前用户没有被赋予角色权限或登录已经过期，请重新<a href='Default.aspx' target='_self'>登录</a>或请联系管理员.",
            };
            panel.Items.Add(msg);
            tpEvaluation.Items.Add(panel);
        }

        protected void CreateTabs()
        {
            Roles role = Utility.Role(this.Page);
            DataTable dt = evaluationManager.GetTabsByRoleName(Enum.GetName(typeof(Roles),role));
            foreach (DataRow row in dt.Rows)
            {
                Ext.Net.Panel panel = CreateTab(row["Id"].ToString(), String.IsNullOrEmpty(row["TabName"] as string) ? "Unknown" : row["TabName"] as string);
                panel.Listeners.Activate.Handler = "Ext.net.DirectMethods.InitTeamMemberEvaluationContent(item);";
                tpEvaluation.Items.Add(panel);
            }
        }

        [DirectMethod]
        public void InitTeamMemberEvaluationContent(Ext.Net.Panel sender)
        {
            if (ViewState[sender.ID] == null)
            {
                ViewState[sender.ID] = sender.ID;
                Groups[] groups = Utility.Groups(this.Page);
                foreach (Groups group in groups)
                {
                    DataTable summaries = evaluationManager.GetTabsByRoleName(Enum.GetName(typeof(Groups), group));
                    TabPanel tp = new TabPanel() { 
                        ID = group + "GroupTab",
                        Plain = true,
                        Margins= "5",
                        Height = 550
                    };
                    sender.Items.Add(tp);
                    foreach(DataRow row in summaries.Rows)
                    {
                        Ext.Net.Panel panel = CreateTab(row["Id"].ToString(), String.IsNullOrEmpty(row["TabName"] as string) ? "Unknown" : row["TabName"] as string);


                        tp.Items.Add(panel);
                    }
                }
                

            }
        }
    }
}